<?php
namespace app\daynote\controller;

use app\common\model\Daynote as DaynoteModel;

class Index extends Base
{
    private $daynoteModel;
    
    // act参数对应操作
    const DAYNOTE_ACTION = [
        0 => 'add',
        1 => 'update',
        2 => 'del',
    ];
    const DAYNOTE_ACTION_R = [
        'add'    => 0,
        'update' => 1,
        'del'    => 2,
    ];
    
    // 每页加载数量
    const PERPAGE_DATA_NUM = 4;
    
    public function __construct() {
        parent::__construct();

        $this->daynoteModel = new DaynoteModel();
    }
    
    public function index()
    {
        return '<br>daynote访问错误';
    }
    
    // 判断操作权限
    private function checkUserAuthToDaynote($user_id = 0, $did = 0) {
        
        if($did <= 0){
            $this->coutHandler->outputJson(lang('wrong did'), 250);
        }else{
            
            $res = $this->daynoteModel->getDayNoteInfoById($did, 'user_id');
            // 判断对应日程是否存在
            if($res != NULL){
                // 判断对应日程是否当前用户可操作
                if($res['user_id'] != $GLOBALS['current_user_id']){
                    $this->coutHandler->outputJson(lang('wrong user to did'), 250);
                }
            }else{
                $this->coutHandler->outputJson(lang('daynote not exist'), 250);
            }
        }
    }
    
    // 对外接口-获取未过期的日程列表
    public function getDayNoteListAction() {
        
        $data = array();
        
        // ->all() 将分页数据转成实际数据数组，否则不能修改
        // ->render() 获取分页显示
        $data = $this->daynoteModel->field('id,rtime,content')
            ->where('user_id', $GLOBALS['current_user_id'])
            ->whereTime('rtime', '>', date('Y-m-d H:i:00'))
            ->paginate(self::PERPAGE_DATA_NUM);
        
        return $this->coutHandler->outputJson(lang('custom success'), 200, $data);
    }
    
    // 对外接口-获取日程数据
    public function getDayNoteInfoAction() {
        
        $did  = $this->request->param('did', 0); // 日程ID
        
        $data = array();
        
        $this->checkUserAuthToDaynote($GLOBALS['current_user_id'], $did);
        
        $data = $this->daynoteModel->field('id,rtime,content')->get($did);
        
        // 处理日期数据
        $temp_rtime_txt = substr($data['rtime'], 0, -3); // 删除秒
        $temp_rtime_arr = explode(' ', $temp_rtime_txt); // 分开日期 时间
        $temp_rtime_date_arr = explode('-', $temp_rtime_arr[0]); // 分开年月日
        $temp_rtime_time_arr = explode(':', $temp_rtime_arr[1]); // 分开时分
        $data['rtimeTxt'] = $temp_rtime_date_arr[0] . '年 ' . $temp_rtime_date_arr[1] . '月 ' . $temp_rtime_date_arr[2] . '日 ' . $temp_rtime_time_arr[0] . '时 '  . $temp_rtime_time_arr[1] . '分';
        
        return $this->coutHandler->outputJson(lang('custom success'), 200, $data);
    }
    
    // 对外接口-新增/修改日程
    // step1: 判断参数正确性
    // step2: 处理过滤$info中的输入参数，避免攻击(此处在config/app.php中default_filter加以规定)
    // step3: 拼装数据
    // step4: 根据$act，进行对应新增/修改操作
    // step5: 返回json结果集
    public function operateDayNoteAction() {
        $act  = $this->request->param('act', 'none');
        $did  = $this->request->param('did', 0); // 日程ID
        $info = $this->request->param('info', array());
        
        // 禁止GET方式提交
        $this->commonCtl->notAllowGetMethod();
        
        // step1
        // 判断操作参数是否有预设
        if(!in_array($act, self::DAYNOTE_ACTION)){
            $this->coutHandler->outputJson(lang('wrong act'), 250);
        }
        if(self::DAYNOTE_ACTION_R[$act] != self::DAYNOTE_ACTION_R['add'] && self::DAYNOTE_ACTION_R[$act] != self::DAYNOTE_ACTION_R['update']){
            $this->coutHandler->outputJson(lang('wrong act'), 250);
        }
        // 判断必要内容是否存在
        if(!isset($info['content']) || !isset($info['rtime'])){
            $this->coutHandler->outputJson(lang('wrong info'), 250);
        }
        // 判断日程是否存在，并且当前用户权限可操作
        if(self::DAYNOTE_ACTION_R[$act] == self::DAYNOTE_ACTION_R['update']){
            $this->checkUserAuthToDaynote($GLOBALS['current_user_id'], $did);
        }
        
        // step2 此处在config/app.php中default_filter加以规定
        
        // step4
        switch (self::DAYNOTE_ACTION_R[$act]){
            case self::DAYNOTE_ACTION_R['add']:
                // step3
                $data = array();
                $data['user_id'] = $GLOBALS['current_user_id'];
                $data['content'] = $info['content'];
                $data['ctime'] = date('Y-m-d H:i:s');
                $data['rtime'] = $info['rtime'];
                
                // 新增
                $res = $this->daynoteModel->save($data);
                
                if($res){
                    $this->coutHandler->outputJson(lang('add daynote success'), 200, array('id' => $this->daynoteModel->id));
                }else{
                    $this->coutHandler->outputJson(lang('add daynote failed'), 250);
                }
                break;
            case self::DAYNOTE_ACTION_R['update']:
                // step3
                $data = array();
                $data['content'] = $info['content'];
                $data['rtime'] = $info['rtime'];
                
                // 修改
                $where = array('id' => $did);
                $res = $this->daynoteModel->save($data, $where);
                if($res){
                    $this->coutHandler->outputJson(lang('edit daynote success'), 200, array('id' => $did));
                }else{
                    $this->coutHandler->outputJson(lang('edit daynote failed'), 250);
                }
                break;
            default:
                $this->coutHandler->outputJson(lang('wrong act'), 250);
                break;
        }
    }
    
    // 对外接口-删除日程
    public function delDayNoteAction() {
        $act  = $this->request->param('act', 'none');
        $did  = $this->request->param('did', 0); // 日程ID
        
        // 禁止GET方式提交
        //$this->commonCtl->notAllowGetMethod();
        
        // step1
        // 判断操作参数是否有预设
        if(!in_array($act, self::DAYNOTE_ACTION)){
            $this->coutHandler->outputJson(lang('wrong act'), 250);
        }
        // 判断操作参数是否为删除
        if(self::DAYNOTE_ACTION_R[$act] != self::DAYNOTE_ACTION_R['del']){
            $this->coutHandler->outputJson(lang('wrong act'), 250);
        }
        // 判断必要内容是否存在
        if($did == 0){
            $this->coutHandler->outputJson(lang('wrong did'), 250);
        }
        // 判断日程是否存在，并且当前用户权限可操作
        $this->checkUserAuthToDaynote($GLOBALS['current_user_id'], $did);
        
        // 删除
        $res = $this->daynoteModel->get($did)->delete();
        //$res = true;
        
        if($res){
            $this->coutHandler->outputJson(lang('del daynote success'), 200);
        }else{
            $this->coutHandler->outputJson(lang('del daynote failed'), 250);
        }
        
    }
}
